const express = require('express');

const router = express.Router();
const proxy = require('express-http-proxy');
const proxyList = require('./proxyList.js');

const API_VERSION = '/v1';

// 依据 proxy_list.js 文件自动生成跳转规则
// TODO 确认接口版本控制
Object.keys(proxyList).forEach(proxyItem => {
  router.use(
    `/PROXY-${proxyItem}`,
    proxy(proxyList[proxyItem].ip, {
      proxyReqPathResolver(req) {
        // console.log('proxyReqPathResolver', result);
        if (proxyList[proxyItem].versionControl) {
          return `${proxyList[proxyItem].class}/api${API_VERSION}${req.url}`;
        }
        return `${proxyList[proxyItem].class}${req.url}`;
      }
    })
  );
});

module.exports = router;
